#include <bits/stdc++.h>
// 2025/06/09
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

const int N = 6e3 + 10;

int n, r[N], f[N][2], in[N];
vector<int> child[N];

void dfs(int u){
    for (int v : child[u]){
        dfs(v);
        f[u][0] += max(f[v][0], f[v][1]);
        f[u][1] += f[v][0];
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> r[i];
        f[i][1] = r[i];
    }
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        child[v].push_back(u);
        in[u]++;
    }
    int root = 0;
    for (int i = 1; i <= n; i++)
    {
        if (in[i] == 0)
        {
            root = i;
            break;
        }
    }
    dfs(root);
    cout << max(f[root][0], f[root][1]) << endl;
    return 0;
}